package com.sense.kukoo.biz_me.adapter.provider

import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import com.sense.kukoo.biz_me.R
import com.sense.kukoo.biz_me.adapter.FollowingsListAdapter
import com.sense.kukoo.common.repo.biz.FollowingsListItemBean
import com.sense.kukoo.common.repo.http.bean.UserSearchBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.utils.DataTimeUtil
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class FollowingsItemProvider(
    val listener: FollowingsListAdapter.FollowingsEventListener
): MultiItemProvider<FollowingsListItemBean>() {

    override fun getItemType(): Int = FollowingsListItemBean.TYPE_DEF

    override fun getItemLayoutId(): Int = R.layout.biz_me_item_followings

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: FollowingsListItemBean) {
        val data: UserSearchBean = item.data
        //
        val iv_icon_channel = viewHolder.getView(R.id.iv_icon_channel) as ImageView
        Monet.get(iv_icon_channel.context)
            .load<GlideConfig>(data.avatarUrl)
            .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
            .circleCrop()
            .into(iv_icon_channel)
        iv_icon_channel.setOnClickListener {
            listener.onBtnChannelClick(data)
        }
        //
        val ll_btn_following = viewHolder.getView(R.id.ll_btn_following)
        val tv_btn_following = viewHolder.getView(R.id.tv_btn_following) as TextView
        if (data.isFollow()) {
            ll_btn_following.setBackgroundResource( com.sense.kukoo.common_design.R.drawable.design_bg_btn_follow_active)
            tv_btn_following.setTextColor(ApplicationContext.getColor(com.sense.kukoo.common_design.R.color.text_active))
            tv_btn_following.text = ApplicationContext.getString( com.sense.kukoo.common_i18n.R.string.following)
        } else {
            ll_btn_following.setBackgroundResource( com.sense.kukoo.common_design.R.drawable.design_bg_btn_follow_def)
            tv_btn_following.setTextColor(ApplicationContext.getColor(com.sense.kukoo.common_design.R.color.text_white))
            tv_btn_following.text = ApplicationContext.getString( com.sense.kukoo.common_i18n.R.string.follow)
        }
        ll_btn_following.setOnClickListener {
            listener.onBtnFollowClick(data)
        }
        //
        val ll_btn_channel = viewHolder.getView(R.id.ll_btn_channel)
        ll_btn_channel.setOnClickListener {
            listener.onBtnChannelClick(data)
        }
        val tv_name_channel = viewHolder.getView(R.id.tv_name_channel) as TextView
        tv_name_channel.text = data.channelName
        //
        val tv_time_updated = viewHolder.getView(R.id.tv_time_updated) as TextView
        if(!TextUtils.isEmpty(data.contentModifiedAt)){
            val time = DataTimeUtil.dateTransform1(data.contentModifiedAt!!)
            tv_time_updated.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.me_followings_list_updated_hint, time)
        }
    }
}